/**
 * Created by sunyxq on 16/3/30.
 */
angular.module('hyt.manageKaiDanService',[])

    .factory('AddCydFactory',function($resource,$rootScope,ENV,Storage){
        var api =  Storage.get('fwqdz'),result,spResult,shfs,fkfs,ddz,isAuto,autoCydh,autoCydh1,count,count1,ddzPhone,ddzPhone1,dhszmsg,baofei;
        console.log(api);

        var resource = $resource(api);
        var pagesize=10;
        var bz={};

        //添加承运单
        return{
            saveCyd : function(dw_id,cyd,yh_id,sid,user_id,cyrq,zd_id){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME : 'sqlKeyBiz.transInsert#addCyd',
                    ACTION_INFO : {
                        'yhmc':cyd.yhmc,
                        'lxdh':cyd.lxdh,
                        'dw_id': dw_id,
                        'zd_id':zd_id,
                        'bmmc':cyd.bmmc,
                        'bm_id':cyd.bm_id,
                        'shbmmc':cyd.shbmmc,
                        'shbm_id':cyd.shbm_id,
                        'cyrq':cyrq,
                        'cydh': cyd.cydhbc,
                        'yh_id': yh_id,
                        'cylx': cyd.cylx,
                        'shfs': cyd.shfs,
                        'shrmc': cyd.shrmc,
                        'shrdh': cyd.shrdh,
                        'shrdz': cyd.shrdz,
                        'fhrmc': cyd.fhrmc,
                        'thf':cyd.thf,
                        'fhrdh': cyd.fhrdh,
                        'fhrdz': cyd.fhrdz,
                        'mdd': cyd.mdd,
                        'hwmc': cyd.hwmc,
                        'sl': cyd.sl,
                        'zl': cyd.zl,
                        'tj': cyd.tj,
                        'bzlx': cyd.bzlx,
                        'bz': cyd.bz,
                        'yf_xf': cyd.yf_xf,
                        'yf_hf': cyd.yf_hf,
                        'yf_yj': cyd.yf_yj,
                        'yf_tf': cyd.yf_tf,
                        'yf_ycdk': cyd.yf_ycdk,
                        'xxf_xf': cyd.xxf_xf,
                        'xxf_qf': cyd.xxf_qf,
                        'dsk_hf': cyd.dsk_hf,
                        'shf1': cyd.shf,
                        'be': cyd.be,
                        'bf': cyd.bf,
                        'tbzt':cyd.tbzt,
                        'yfdfzt':cyd.yfdfzt,
                        'dskdfzt':cyd.dskdfzt,
                        'yf_df': cyd.yf_df,
                        'dsk_xf': cyd.dsk_xf,
                        'qdfs':cyd.qdfs,
                        'sfyqd':cyd.sfyqd
                    }
                },function(r){
                    result=r;
                    console.log(r);
                    $rootScope.$broadcast('SaveCyd.CydUpdated');
                })
            },
            getResult: function(){
                return result;
            },
            saveAndPrint : function(dw_id,cyd,yh_id,sid,user_id,cyrq,zd_id){
            return resource.save({
                SID:sid,
                USER_ID:user_id,
                ACTION_NAME : 'sqlKeyBiz.transInsert#addCyd',
                ACTION_INFO : {
                    'yhmc':cyd.yhmc,
                    'lxdh':cyd.lxdh,
                    'dw_id': dw_id,
                    'zd_id':zd_id,
                    'bmmc':cyd.bmmc,
                    'bm_id':cyd.bm_id,
                    'shbmmc':cyd.shbmmc,
                    'shbm_id':cyd.shbm_id,
                    'cyrq':cyrq,
                    'cydh': cyd.cydhbc,
                    'yh_id': yh_id,
                    'cylx': cyd.cylx,
                    'shfs': cyd.shfs,
                    'shrmc': cyd.shrmc,
                    'shrdh': cyd.shrdh,
                    'shrdz': cyd.shrdz,
                    'fhrmc': cyd.fhrmc,
                    'fhrdh': cyd.fhrdh,
                    'fhrdz': cyd.fhrdz,
                    'mdd': cyd.mdd,
                    'hwmc': cyd.hwmc,
                    'sl': cyd.sl,
                    'zl': cyd.zl,
                    'tj': cyd.tj,
                    'bzlx': cyd.bzlx,
                    'bz': cyd.bz,
                    'yf_xf': cyd.yf_xf,
                    'yf_hf': cyd.yf_hf,
                    'yf_yj': cyd.yf_yj,
                    'yf_tf': cyd.yf_tf,
                    'yf_ycdk': cyd.yf_ycdk,
                    'xxf_xf': cyd.xxf_xf,
                    'xxf_qf': cyd.xxf_qf,
                    'dsk_hf': cyd.dsk_hf,
                    'shf1': cyd.shf,
                    'thf': cyd.thf,
                    'be': cyd.be,
                    'bf': cyd.bf,
                    'tbzt':cyd.tbzt,
                    'yfdfzt':cyd.yfdfzt,
                    'dskdfzt':cyd.dskdfzt,
                    'yf_df': cyd.yf_df,
                    'dsk_xf': cyd.dsk_xf,
                    'qdfs':cyd.qdfs,
                    'sfyqd':cyd.sfyqd
                }
            },function(r){
                spResult=r;
                console.log(r);
                $rootScope.$broadcast('SaveAndPrintCyd.CydUpdated');
            })
        },
        getResult1: function(){
            return spResult;
        },
            //承运单号是否自动生成
            checkAuto:function(sid,yh_id,user_id,zd_id){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME : 'sqlKeyBiz.list#queryBmAppSet',
                    ACTION_INFO : {
                        'id':yh_id+""+zd_id,
                        'bm_id':zd_id,
                        'yh_id':yh_id
                    }
                },function(r){
                    console.log(r);
                    isAuto = r;
                    $rootScope.$broadcast('CheckAutoCydh');
                });
            },
            isAutoResult: function(){
                return isAuto;
            },
            //获取单号设置信息
            queryDhsz:function(sid,yh_id,user_id,zd_id,dw_id){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME : 'sqlKeyBiz.list#queryDjbhPz',
                    ACTION_INFO : {
                        'dw_id':dw_id,
                        'zd_id':zd_id,
                        'yh_id':yh_id
                    }
                },function(r){
                    console.log(r);
                    dhszmsg = r;
                    $rootScope.$broadcast('GetDhszmsg');
                });
            },
            getDhszmsg: function(){
                return dhszmsg;
            },
            //获取生成的承运单号
            getCydh:function (sid,yh_id,user_id,zd_id,dw_id){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME : 'sqlKeyBiz.getAutoCydh#queryDjbhPz',
                    ACTION_INFO : {
                        'zd_id':zd_id,
                        'dw_id':dw_id,
                        'yh_id':yh_id
                    }
                },function(r){
                    console.log(r);
                    autoCydh = r;
                    $rootScope.$broadcast('GetAutoCydh');
                });
            },
            getCydhResult:function(){
                return autoCydh;
            },
            getCydh1:function (sid,yh_id,user_id,zd_id,dw_id){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME : 'sqlKeyBiz.getAutoCydh#queryDjbhPz',
                    ACTION_INFO : {
                        'zd_id':zd_id,
                        'dw_id':dw_id,
                        'yh_id':yh_id
                    }
                },function(r){
                    console.log(r);
                    autoCydh1 = r;
                    $rootScope.$broadcast('GetAutoCydh1');
                });
            },
            getCydhResult1:function(){
                return autoCydh1;
            },
            //初始化送货方式
            queryShfs : function(sid,yh_id,user_id){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME : 'sqlKeyBiz.list#y_param_set_query',
                    ACTION_INFO : {
                        'yh_id':yh_id,
                        'type':'shfs'
                    }
                },function(r){
                        console.log(r);
                        shfs = r.ACTION_INFO.RESULT_LIST;
                        $rootScope.$broadcast('InitData');

                });
            },
            getShfs:function(){
                return shfs;
            },

            //初始化付款方式
            queryFkfs : function(sid,yh_id,user_id){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME :  'sqlKeyBiz.list#y_param_set_query',
                    ACTION_INFO : {
                        'type':'fkfs',
                        'yh_id':yh_id
                    }
                },function(r){
                    if(r.ACTION_RETURN_CODE == "000000") {
                        console.log(r);
                        fkfs = r.ACTION_INFO.RESULT_LIST;
                        $rootScope.$broadcast('InitData');
                    }
                });
            },
            getFkfs:function(){
                return fkfs;
            },

            //初始化到达站
            queryDdz:function(sid,yh_id,dw_id,user_id,zd_id){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME :  'sqlKeyBiz.list#bmxx_query_by_zd_id',
                    ACTION_INFO : {
                        'dw_id':dw_id,
                        'zd_id':zd_id,
                        'yh_id':yh_id
                    }
                },function(r){
                    if(r.ACTION_RETURN_CODE == "000000") {
                        console.log(r);
                        ddz = r.ACTION_INFO.RESULT_LIST;
                        $rootScope.$broadcast('InitData');
                    }
                });
            },
            getDdz:function(){
                return ddz;
            },

            //初始化包装
            queryBz:function(sid,dw_id,yh_id,user_id,bzmc){
                var hasNextPage =true;
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME :  'sqlKeyBiz.page#baozhuang_list',
                    ACTION_INFO : {
                        'dw_id':dw_id,
                        'pagesize':'10',
                        'current_page':'1',
                        'yh_id':yh_id,
                        'mc':bzmc
                    }
                },function(r){
                    if(r.ACTION_RETURN_CODE == "000000") {
                        if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }
                        console.log(r);
                        bz[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : 2,
                            'data' : r.ACTION_INFO.RESULT_LIST
                        };
                        $rootScope.$broadcast('InitData');
                    }
                });
            },
            getBzList : function(){
                if(bz[pagesize] == undefined){
                    return false;
                }
                return bz[pagesize];
            },
            getMoreBz : function(sid,dw_id,yh_id,user_id,bzmc){
                if(bz[pagesize] == undefined){
                    return false;
                }

                //获取以前的数据
                var hasNextPage = bz[pagesize].hasNextPage;
                var current_page = bz[pagesize].current_page;
                var moreBzListData = bz[pagesize].data;
                resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME :  'sqlKeyBiz.page#baozhuang_list',
                    ACTION_INFO : {
                        'dw_id':dw_id,
                        'pagesize':pagesize,
                        'current_page':current_page,
                        'yh_id':yh_id,
                        'mc':bzmc
                    }
                },function(r){
                    current_page++;
                    if(r.ACTION_RETURN_CODE == "000000") {
                        if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }

                        moreBzListData = moreBzListData.concat(r.ACTION_INFO.RESULT_LIST);

                        bz[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : current_page,
                            'data' : moreBzListData
                        };
                        $rootScope.$broadcast('InitData');
                    }
                });
            },
            hasNextPage: function() {
                if (bz[pagesize] === undefined) {
                    return false;
                }
                return bz[pagesize].hasNextPage;
            },
            //判断承运单号是否重复
            checkCydhExist:function(sid,user_id,dw_id,yh_id,cydh,cyd_id){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME :'sqlKeyBiz.list#count_cyd_by_cydh',
                    ACTION_INFO : {
                        'dw_id':dw_id,
                        'yh_id':yh_id,
                        'cydh':cydh,
                        'cyd_id':cyd_id
                    }
                },function(resp){
                    count=resp;
                    console.log(resp);
                    $rootScope.$broadcast('checkCydh');
                })
            },
            getCount:function(){
                return count;
            },
            //判断承运单号是否重复
            checkCydhExist1:function(sid,user_id,dw_id,yh_id,cydh,cyd_id){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME :'sqlKeyBiz.list#count_cyd_by_cydh',
                    ACTION_INFO : {
                        'dw_id':dw_id,
                        'yh_id':yh_id,
                        'cydh':cydh,
                        'cyd_id':cyd_id
                    }
                },function(resp){
                    count1=resp;
                    console.log(resp);
                    $rootScope.$broadcast('checkCydh1');
                })
            },
            getCount1:function(){
                return count1;
            },
            getDdzPhone:function(sid,user_id,bm_id,yh_id){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME :'sqlKeyBiz.list#bmxx_query',
                    ACTION_INFO : {
                        'bm_id':bm_id,
                        'yh_id':yh_id
                    }
                },function(resp){
                    ddzPhone=resp;
                    console.log(resp);
                    $rootScope.$broadcast('getPhone.ddz');
                })
            },
            getDdzPhoneResult:function(){
                return ddzPhone;
            },
            getDdzPhone1:function(sid,user_id,bm_id,yh_id){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME :'sqlKeyBiz.list#bmxx_query',
                    ACTION_INFO : {
                        'bm_id':bm_id,
                        'yh_id':yh_id
                    }
                },function(resp){
                    ddzPhone1=resp;
                    console.log(resp);
                    $rootScope.$broadcast('getPhone1.ddz');
                })
            },
            getDdzPhoneResult1:function(){
                return ddzPhone1;
            },
            //保费计算
            getbaofei:function(sid,user_id,yh_id,dw_id,be){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME : 'locationBiz.bfjs',
                    ACTION_INFO : {
                        'dw_id':dw_id,
                        'yh_id':yh_id,
                        'be':be
                    }
                },function(r){
                    console.log(r);
                    baofei = r.ACTION_INFO.bfje;
                    $rootScope.$broadcast('Baofeijisuan');
                });
            },
            baofeiResult: function(){
                return baofei;
            }
        }
    });
